package ru.rchervot.sanasar.server;

import ru.rchervot.sanasar.client.model.QueryJoin;
import ru.rchervot.sanasar.client.model.QueryJoinAnd;
import ru.rchervot.sanasar.client.model.QueryJoinOr;

public abstract class AbstractServerQuery implements ServerQuery {

	final protected StringBuffer SQLQuery = new StringBuffer();
//	final protected org.apache.lucene.search.BooleanQuery luceneQuery = new BooleanQuery();
	
	final protected static String SQL_OR = " OR ";
	final protected static String SQL_AND = " AND ";
	final protected static String SQL_AND_NOT = " AND NOT ";
	
	private QueryJoin joinOperator = new QueryJoinAnd();

//	@Override
//	public Query getLuceneQuery() {
//		// TODO Auto-generated method stub
//		return null;
//	}

	@Override
	public String getSQLCommand() {
		return SQLQuery.toString();
	}

//	@Override
//	public void addLuceneQuery(Query q) {
//		switch(joinOperator) {
//		case AND: luceneQuery.add(q, Occur.MUST); break;
//		case OR: luceneQuery.add(q, Occur.SHOULD); break;
//		}
//	}

	@Override
	public void addSQLClause(String s) {
		if (SQLQuery.length() > 0) {
			if (joinOperator instanceof QueryJoinAnd) {
				SQLQuery.append(SQL_AND);
			} else if (joinOperator instanceof QueryJoinOr) {
				SQLQuery.append(SQL_OR);
			}
		}
		SQLQuery.append("(" + s + ") ");
	}

	@Override
	public void setJoinOperator(QueryJoin jo) {
		joinOperator = jo;
	}
	
	
}
